import java.util.ArrayList;
import java.util.List;
import java.util.Random;

// 定义ListNode类
class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class Solution {
    List<Integer> list;
    Random random;

    public Solution(ListNode head) {
        list = new ArrayList<Integer>();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }
        random = new Random();
    }

    public int getRandom() {
        return list.get(random.nextInt(list.size()));
    }

    public static void main(String[] args) {
        // 创建链表 1->2->3
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);

        // 初始化Solution对象
        Solution solution = new Solution(head);

        // 测试 getRandom 方法
        for (int i = 0; i < 10; i++) {
            System.out.println(solution.getRandom());
        }
    }
}